from public.action import *
from public.tools import *
from public.testData import *
"""
一个购物流程的案例
包含，购物，发货，确认收货三个环节
"""
# 用例标题
testCase = "dbshopST003_购物支付方式为_货到付款支付流程"

kuaidi = "00100001111"
kuaidiExplain = "已发货"
orderID = "1659922234920002"


# 操作步骤
# 打开浏览器
firefox=getDriver()
openBrowser(firefox,url)
# 登录
login(firefox,name,password)
# 搜索商品
firefox.find_element_by_xpath("//input[@name='keywords']").send_keys("小米")
firefox.find_element_by_xpath("//input[@class='search-btn iconfont']").click()
# 进入商品详情页面
firefox.find_element_by_xpath("//img[@alt='小米Mix3 全网通版 8GB+128GB 黑色 磁动力滑盖全面屏']").click()
firefox.switch_to.window(firefox.window_handles[-1])
# 加入购物车
firefox.find_element_by_id("add_cart_submit").click()
#5.结算
firefox.find_element_by_link_text("去购物车结算").click()
firefox.find_element_by_link_text("去结算").click()
firefox.find_element_by_xpath("//input[@class='btn btn-large btn-primary']").click()
firefox.find_element_by_xpath("//input[@value='hdfk']").click()
firefox.find_element_by_xpath("//input[@value='确认订单']").click()
#保存订单号
shopid=firefox.find_element_by_xpath("(//font[@color='color:#ED145B'])[2]").text
print("订单编号:",shopid)

print("购物完成")
firefox.quit()
# 后台系统操作
firefox=getDriver()

openBrowser(firefox,"http://localhost/dbshop/admin")
logInAdmin(firefox)
# 搜索订单
firefox.find_element_by_link_text("销售管理").click()
firefox.find_element_by_link_text("订单管理").click()
firefox.find_element_by_xpath("//input[@class='span11']").send_keys(shopid)
firefox.find_element_by_xpath("(//button[@class='btn btn-small btn-primary'])[1]").click()
firefox.find_element_by_link_text("编辑查看").click()


# 发货
firefox.find_element_by_link_text("发货操作").click()
firefox.find_element_by_xpath("//input[@id='express_number']").send_keys(kuaidi)
firefox.find_element_by_id("state_info").send_keys(kuaidiExplain)
firefox.find_element_by_xpath("//button[@class='btn btn-small btn-primary']").click()
print("发货完成")


# 付款
firefox.find_element_by_link_text("付款操作").click()
firefox.find_element_by_xpath("//select[@id='pay_state']/option[3]").click()
firefox.find_element_by_id("state_info").send_keys("已付款")
firefox.find_element_by_xpath("//button[@class='btn btn-small btn-primary']").click()
firefox.quit()

# 前台收货操作
firefox=getDriver()
openBrowser(firefox,"http://localhost/dbshop")
#登录
login(firefox,name,password)

firefox.find_element_by_link_text("我的订单").click()
firefox.find_element_by_partial_link_text("已发货").click()
firefox.find_element_by_xpath("//input[@placeholder='输入订单号']").send_keys(shopid)
firefox.find_element_by_xpath("(//button[@class='btn btn-small btn-primary'])[1]").click()
firefox.find_element_by_xpath("(//button[@class='btn btn-small btn-primary'])[2]").click()
firefox.find_element_by_xpath("//button[@class='btn btn-primary btn-large']").click()
firefox.switch_to.alert.accept()
print("收货成功")

# 断言
try:
    firefox.find_element_by_link_text(shopid).is_displayed()  #is_displayed()函数判断元素是否显示
    writeLog("%s 测试通过"%testCase)
except:
    writeLog("%s 测试失败" % testCase)
    getPhoto(firefox)
firefox.quit()
print("完成购买")


